package com.serkol.wx.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.hutool.core.date.DateUtil;
import com.serkol.wx.bean.AgentSuccessMsg;
import com.serkol.wx.bean.OrderSuccessMsg;
import com.serkol.wx.bean.TiChengMsg;
import com.serkol.wx.bean.WithdrawalAppervalMsg;
import com.serkol.wx.service.WechatMiniMessageService;
import com.serkol.wx.service.WechatMpMessageService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Map.Entry;

@Service
public class WechatMpMessageServiceImpl implements WechatMpMessageService {
    @Autowired
    private WxMpService wxMpService;

    @Override
    public boolean sendMessage(OrderSuccessMsg msg)  {
        String templateId = "3jBJewun6gv8l-2JtnSfMo9OVNco6HWiVoC_7mXzoU8";


        Map<String, Object> map = new HashMap<>();
        map.put("thing13", msg.getThing13());
        map.put("thing11", msg.getThing11());
        map.put("amount5", msg.getAmount5() + "");
        //  map.put("const8", msg.getConst8());
        map.put("time6", msg.getTime6());

        sendTemplateMsg(templateId, msg.getOpenId(), map);
        return true;
    }

    @Override
    public boolean sendWithdrawalAppervalMessage(WithdrawalAppervalMsg msg) {
        String templateId = "DFFDpIWvraQzJxpsty6Ccr2OIDKt5sJbVxGquDHsadE";
        Map<String, Object> map = new HashMap<>();
        map.put("thing1", msg.getThing1());
        map.put("time2", msg.getTime2());
        map.put("amount3", msg.getAmount3() + "");
        sendTemplateMsg(templateId, msg.getOpenId(), map);


        return true;
    }

    @Override
    public boolean sendTiChengMessage(TiChengMsg msg) {
        String templateId = "m1qkNl_DZlEJ09DR97mtFdTJ-CzZ81_HxCN96eYeeZY";
        Map<String, Object> map = new HashMap<>();

        map.put("thing3", msg.getThing3());
        map.put("thing4", msg.getThing4());
        map.put("thing5", msg.getThing5());
        map.put("amount2", msg.getAmount2().doubleValue() + "");

        sendTemplateMsg(templateId, msg.getOpenId(), map);
        return true;
    }

    private List<WxMpTemplateData> pareseData(Map<String, Object> map) {
        List<WxMpTemplateData> datas = new ArrayList<>();
        for (Entry entry : map.entrySet()) {
            WxMpTemplateData data = new WxMpTemplateData();
            String mapKey = (String) entry.getKey();

            Object mapValue = entry.getValue();

            System.out.println(mapKey + ":" + mapValue);
            data.setName(mapKey);
            data.setValue(mapValue + "");
            datas.add(data);

        }
        return datas;
    }

    private void sendTemplateMsg(String templateId, String openId, Map<String, Object> map) {
        WxMpTemplateMsgService templateMsgService = wxMpService.getTemplateMsgService();
        List<WxMpTemplateData> list = pareseData(map);
        WxMpTemplateMessage uniformMessage = new WxMpTemplateMessage();
        uniformMessage.setToUser(openId);
        //  uniformMessage.setToUser("olOpL5qjhYZJUsVfyy8a9U6wJ19E");

        uniformMessage.setTemplateId(templateId);
        uniformMessage.setData(list);
        try {
            String s = templateMsgService.sendTemplateMsg(uniformMessage);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }

    }
}
